 // Global Ajax events can be handled on every request!
Ext.Ajax.on( {
	'beforerequest' : function() {
		Ext.getBody().mask('正在加载...','x-mask-loading');
	},
	'requestcomplete' : function() {
		Ext.getBody().unmask();
	},
	'requestexception':function(){	
		Ext.getBody().unmask();
		Ext.Msg.alert("提示信息", "连接服务器失败!",function(){Ext.getBody().unmask();});		
	}	
});	

/**
 * VTypes
 */

Ext.form.VTypes["hostnameVal1"] = /^[a-zA-Z][-.a-zA-Z0-9]{0,254}$/;
Ext.form.VTypes["hostnameVal2"] = /^[a-zA-Z]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}([.][a-zA-Z]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}){0,}$/;
Ext.form.VTypes["ipVal"] = /^([1-9][0-9]{0,1}|1[013-9][0-9]|12[0-689]|2[01][0-9]|22[0-3])([.]([1-9]{0,1}[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){2}[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-4])$/;
Ext.form.VTypes["netmaskVal"] = /(^(128|192|224|24[08]|25[245])\.0\.0\.0$)|(^255\.(0|128|192|224|24[08]|25[245])\.0\.0$)|(^255\.255\.(0|128|192|224|24[08]|25[245])\.0$)|(^255\.255\.255\.(0|128|192|224|24[08]|252)$)/;
Ext.form.VTypes["portVal"] = /^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
Ext.form.VTypes["multicastVal"] = /^((22[5-9]|23[0-9])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3})|(224[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])){2})|(224[.]0[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])))$/;
Ext.form.VTypes["usernameVal"] = /^[a-zA-Z][-_.a-zA-Z0-9]{0,30}$/;
Ext.form.VTypes["passwordVal1"] = /^.{6,31}$/;
Ext.form.VTypes["passwordVal2"] = /[^a-zA-Z].*[^a-zA-Z]/;
/*hostname*/
Ext.form.VTypes["hostname"] = function(v) {
	if (!Ext.form.VTypes["hostnameVal1"].test(v)) {
		Ext.form.VTypes["hostnameText"] = "Must begin with a letter and not exceed 255 characters"
		return false;
	}
	Ext.form.VTypes["hostnameText"] = "L[.L][.L][.L][...] where L begins with a letter, ends with a letter or number, and does not exceed 63 characters";
	return Ext.form.VTypes["hostnameVal2"].test(v);
}
Ext.form.VTypes["hostnameText"] = "Invalid Hostname"
Ext.form.VTypes["hostnameMask"] = /[-.a-zA-Z0-9]/;
/*ip*/
Ext.form.VTypes["ip"] = function(v) {
	return Ext.form.VTypes["ipVal"].test(v);
}
Ext.form.VTypes["ipText"] = "1.0.0.1 - 223.255.255.254 excluding 127.x.x.x"
Ext.form.VTypes["ipMask"] = /[.0-9]/;
/*netmask*/
Ext.form.VTypes["netmask"] = function(v) {
	return Ext.form.VTypes["netmaskVal"].test(v);
}
Ext.form.VTypes["netmaskText"] = "128.0.0.0 - 255.255.255.252"
Ext.form.VTypes["netmaskMask"] = /[.0-9]/;
/*port*/
Ext.form.VTypes["port"] = function(v) {
	return Ext.form.VTypes["portVal"].test(v);
}
Ext.form.VTypes["portText"] = "0 - 65535"
Ext.form.VTypes["portMask"] = /[0-9]/;
/*multicast*/
Ext.form.VTypes["multicast"] = function(v) {
	return Ext.form.VTypes["multicastVal"].test(v);
}
Ext.form.VTypes["multicastText"] = "224.0.1.0 - 239.255.255.255"
Ext.form.VTypes["multicastMask"] = /[.0-9]/;
/*username*/
Ext.form.VTypes["username"] = function(v) {
	return Ext.form.VTypes["usernameVal"].test(v);
}
Ext.form.VTypes["usernameText"] = "用户名必须以字母开头，且不能超过30个字母"
Ext.form.VTypes["usernameMask"] = /[-_.a-zA-Z0-9]/;
/*password*/
Ext.form.VTypes["password"] = function(v) {
	if (!Ext.form.VTypes["passwordVal1"].test(v)) {
		Ext.form.VTypes["passwordText"] = "密码长度为6-31个字符";
		return false;
	}
	Ext.form.VTypes["passwordText"] = "密码必须包含至少2个数字或符号";
	return Ext.form.VTypes["passwordVal2"].test(v);
}
Ext.form.VTypes["passwordText"] = "密码无效"
Ext.form.VTypes["passwordMask"] = /./;
/*phone*/
Ext.form.VTypes["phoneVal"] = /^((0[1-9]{2,3}[-])?(0[1-9][0-9]{1,2})?[-])?\d{6,8}$/
Ext.form.VTypes["phoneMask"] = /[\d-]/;
Ext.form.VTypes["phoneText"] = '不是有效的电话号码，格式应为：6256335 or 0572-6256335 or 086-0572-6256335 ';
Ext.form.VTypes["phone"] = function(v) {
	return Ext.form.VTypes["phoneVal"].test(v);
}

Ext.override(Ext.menu.DateMenu,{  
     render : function(){  
         Ext.menu.DateMenu.superclass.render.call(this);  
         if(Ext.isGecko){  
             this.picker.el.dom.childNodes[0].style.width = '188px';  
             this.picker.el.dom.style.width = '188px';  
         }  
     }  
 }); 
 
Ext.namespace("RockyCurd");
/**
 * 	1、RockyCurd.JsonLibDate
 */
RockyCurd.JsonLibDate = function(config) {
	RockyCurd.JsonLibDate.superclass.constructor.call(this, config);
}
Ext.extend(RockyCurd.JsonLibDate, Ext.form.DateField, {
	setValue : function(date) {
		if (date && date["time"]) {
			RockyCurd.JsonLibDate.superclass.setValue.call(this,
					new Date(date["time"]));
		} else {
			RockyCurd.JsonLibDate.superclass.setValue.call(this, date);
		}
	},
	format : 'Y-m-d',
	allowBlank : false,
	readOnly : true
});
Ext.reg('jsondatefield', RockyCurd.JsonLibDate);

/**
 * 	2、RockyCurd.TreeLoader
 */
RockyCurd.TreeLoader = function(config) {
	RockyCurd.TreeLoader.superclass.constructor.call(this, config);
}
Ext.extend(RockyCurd.TreeLoader, Ext.tree.TreeLoader, {
	createNode : function(attr) {
		if (this.nodeMeta) {
			if (this.nodeMeta.id) {
				attr.id = attr[this.nodeMeta.id];
			}
			if (this.nodeMeta.text) {
				attr.text = attr[this.nodeMeta.text];
			}
			if (!attr.leaf) {
				attr.leaf = this.nodeMeta.leaf
						? attr[this.nodeMeta.leaf]
						: null;
			}
		}
		return RockyCurd.TreeLoader.superclass.createNode.call(this, attr);
	}
});


/**
 * 	3、树状下拉框2
 */
RockyCurd.TreeComboBox = Ext.extend(Ext.form.ComboBox, {
	constructor:function(config) {
		this.config = Ext.apply( {
			store : new Ext.data.SimpleStore( {
				fields : [],
				data : [[]]
			}),
			mode : 'local',
			triggerAction : 'all',
			maxHeight : 300,
			selectedClass : '',
			border:false,
			id : Ext.id()
		}, config);
		this.treeid = this.config.id + "tree";
		this.tpl = "<tpl for='.'><div style='height:300px'><div id='" + this.treeid + "'></div></div></tpl>";
		RockyCurd.TreeComboBox.superclass.constructor.call(this, this.config);
	}, 
	initComponent : function() {
		RockyCurd.TreeComboBox.superclass.initComponent.call(this);		
		this.on('expand', function() {	
			if(!this.tree.rendered){
			this.tree.render(this.treeid);
			this.tree.expand();
			}else{
								this.tree.root.reload();
								this.tree.root.expand(true);
			}
		}, this);		
	}
});
Ext.reg('treecombo', RockyCurd.TreeComboBox);



/**
 * 4\1、	RockyAbstractCurd
 */
RockyAbstractCurd = {
	create : function() {
		this.showWin();
		this.reset();
	},
	save : function() {
		if(this.fp.form.isValid()){
			var id = this.fp.form.findField("id").getValue();
			var formData = this.fp.form.getValues();
			var jsondata = Ext.util.JSON.encode(formData);
			Ext.Ajax.request( {
				url : this.baseUrl,
				params : {	
					'cmd':(id ? "update" : "create"),				
					'jsondata':jsondata
				},
				method : 'POST',
				success : function(response) {				
					var r = Ext.util.JSON.decode(response.responseText);
					if (!r.success) {
						Ext.Msg.alert("提示信息","失败，由以下原因所致：<br/>"+ (r.errors? r.errors: "未知原因"));
					} else {
						Ext.Msg.alert("提示信息", "保存成功!",function(){
							this.closeWin();
							this.refresh();
						},this);

					}
				},
				failure:function(Optional){				
					Ext.Msg.alert("提示信息", "连接服务器失败!");								 
				}, 
				scope : this
			});
		}
	},
	edit : function() {
		Ext.Msg.alert("提示","请重载该edit方法！");
	},
	removeData : function() {
		Ext.Msg.alert("提示","请重载该removeData方法！");
	},
	refresh:function(){
		Ext.Msg.alert("提示","请重载该refresh方法！");
	},
	search : function() {
		if(this.store){
			this.store.baseParams.start = 0;
		}
		this.refresh();
	},
	showWin : function() {
		if (!this.fp) {
				this.fp = this.createForm();
		}
		if (!this.win) {			
			this.win = this.createWin();
		}		
		this.win.show();
	},	
	createWin : function() {
		return new Ext.Window(Ext.apply( {
			buttonAlign : "center",
			closeAction : "hide",
			modal : true,
			shadow : true,
			items : this.fp,
			buttons : [ {
				text : "保存",
				handler : this.save,
				scope : this
			}, {
				text : "清空",
				handler : this.reset,
				scope : this
			}, {
				text : "取消",
				handler : this.closeWin,
				scope : this
			}]
		}, this.winCfg));
	},	
	createForm:function(){
		Ext.Msg.alert("提示","请重载该createForm方法！");
	},
	reset : function() {
		if (this.win) {
			this.fp.form.reset();
		}
	},
	closeWin : function() {
		if (this.win) {
			this.win.hide();
		}
	}
}
/**
 * 4\2、	RockyPanelCurd
 */
RockyPanelCurd = Ext.extend(Ext.Panel,RockyAbstractCurd);
/**
 * 4\3、	RockyGridCurd
 */
RockyAbstractGridCurd = Ext.extend(Ext.grid.GridPanel,RockyAbstractCurd);
RockyGridCurd = Ext.extend(RockyAbstractGridCurd,{		
	constructor :function(config){
		RockyGridCurd.superclass.constructor.call(this, config);	
		this.on("celldblclick", this.edit, this);
	},
	afterRender : function(){
			RockyGridCurd.superclass.afterRender.call(this);		
			if(this.customTbars){
				this.getTopToolbar().getEl().setStyle('border','0');
				if(!Ext.isArray(this.customTbars)){
					this.customTbars = [this.customTbars];
				}			
				var eachTbarTemp=null;
				Ext.each(this.customTbars, function(eachTbar, index, array) {				
					eachTbarTemp = new Ext.Toolbar(eachTbar.items);
					eachTbarTemp.render(this.tbar.dom.appendChild(document.createElement('div')));
				}, this)
			}
			this.getStore().load();	
	},
	removeData : function() {
		var record = this.getSelectionModel().getSelections();
		if (!record||record.length==0) {
			Ext.Msg.alert("提示", "请先选择要删除的行!");
			return;
		}
		var m = Ext.MessageBox.confirm("删除提示", "是否真的要删除数据？", function(ret) {
			var id = '';
			Ext.each(record,function(node){id+=node.id + "," ;},this);
			if(id.indexOf(",")>0){
				id = id.substring(0,id.length-1);
			}
			if (ret == "yes") {
				Ext.Ajax.request( {
					url : this.baseUrl,
					params : {
						'ids' : id,
						 cmd:"remove",
						'fromadmin':this.fromadmin||''
					},
					method : 'POST',
					success : function(response) {
						var r = Ext.decode(response.responseText);
						if (!r.success) {
							Ext.Msg.alert("提示信息", "数据删除失败，由以下原因所致：<br/>"
									+ (r.errors ? r.errors : "未知原因"));
						} else {
							Ext.Msg.alert("提示信息", "成功删除数据!", function() {
								this.refresh();		
							}, this);
						}
					},
					scope : this
				});
			}
		}, this);
	},
	edit : function() {
		var record = this.getSelectionModel().getSelected();
		if (!record) {
			Ext.Msg.alert("提示", "请先选择要编辑的行!");
			return;
		}
		var id = record.get("id");
		this.showWin();	
		this.fp.form.loadRecord(record);
	},
	refresh : function() {
		this.store.removeAll();
		this.store.reload();
	}
});
/**
 * 4\4、	RockyTreeCurd
 */
RockyAbstractTreeCurd = Ext.extend(Ext.tree.TreePanel,RockyAbstractCurd);
RockyTreeCurd = Ext.extend(RockyAbstractTreeCurd,{
	removeData : function() {
		var record = this.getSelectionModel(). getSelectedNode();
		if (!record) {
			Ext.Msg.alert("提示", "请先选择要删除的记录!");
			return;
		}
		var m = Ext.MessageBox.confirm("删除提示", "是否真的要删除数据？", function(ret) {			
			if (ret == "yes") {
				Ext.Ajax.request( {
					url : this.baseUrl,
					params : {
						'cmd':'remove',
						'id' : record.id,
						'fromadmin':this.fromadmin||''
					},
					method : 'POST',
					success : function(response) {
						var r = Ext.decode(response.responseText);
						if (!r.success) {
							Ext.Msg.alert("提示信息", "数据删除失败，由以下原因所致：<br/>"+ (r.errors ? r.errors : "未知原因"));
						} else {
							Ext.Msg.alert("提示信息", "成功删除数据!", function() {
								this.refresh();		
							}, this);
						}
					},
					scope : this
				});
			}
		}, this);
	},	
	edit : function() {
		var record = this.getSelectionModel(). getSelectedNode();
		if (!record) {
			Ext.Msg.alert("提示", "请先选择要编辑的行!");
			return;
		}
		this.showWin();
		this.fp.form.loadRecord(new Ext.data.Record(record.attributes,record.id));
	},
	refresh :function() {
		this.root.reload();
		this.root.expand(true);
	}
});

RockyCurd.sexStore = new Ext.data.SimpleStore( {
					fields : ["retrunValue", "displayText"],
					data : [['1', '男'], ['0', '女'], ['9', '保密']]
				});